<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_tagihan_telephone extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_tagihan_telephone_model', 'TagihanTelephone');
		//$this->load->model('isp_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Tagihan Telephone";	
		viewByTemplate('pro_tagihan_telephone/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->TagihanTelephone->GetJSON();
	}

	function getdata_detail()
	{

		$data['id']= $this->input->post('id_tagihan_tlp');
		$this->load->model('pro_tagihan_telephone_model',$data);
		//$this->secure_session();
		echo $this->TagihanTelephone->GetJSONdetail(post('id_tagihan_tlp'));
	}

	function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->TagihanTelephone->GetJSONdetail(post('id_tagihan_tlp'));
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->TagihanTelephone->GetJSON2();
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "Tagihan Telephone";
		$data['data'] = NULL;

		$query = $this->db->query("SELECT no_tagihan_tlp FROM (SELECT * FROM pro_tagihan_telephone ORDER BY no_tagihan_tlp DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		$bulan = date("m");
		$tahun = date("Y");

		$hasil = "";
			$iromawi =
			array("","I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII");
			if(array_key_exists($bulan,$iromawi)){
				$hasil = $iromawi[$bulan];
			}

		
		$data['no_tagihan_tlp_auto'] = "KU.01.02/" ."____" . "/PRIM"."/".$hasil."/".$tahun;

		$query1 = $this->db->query("SELECT id_tagihan_tlp FROM (SELECT * FROM pro_tagihan_telephone ORDER BY id_tagihan_tlp DESC) WHERE rownum <=1");
				 				if ($query1->row_array()) {
				 					$getquery1 = $query1->row_array();
				 					$rowcount1 = $getquery1['id_tagihan_tlp'];
				 					$autonum1 = $rowcount1 + 1;
				 				} else {
								$autonum1 = 1;
							}
		$data['id_tagihan_tlp_auto'] = $autonum1;

		$query3 = $this->db->query("SELECT id_tagihan_tlp_dtl FROM (SELECT * FROM pro_tagihan_telephone_dtl ORDER BY id_tagihan_tlp_dtl DESC) WHERE rownum <=1");
				 				if ($query3->row_array()) {
				 					$getquery3 = $query3->row_array();
				 					$rowcount3 = $getquery3['id_tagihan_tlp_dtl'];
				 					$autonum3 = $rowcount3 + 1;
				 				} else {
								$autonum3 = 1;
							}
		$data['id_tagihan_tlp_dtl_auto'] = $autonum3;
		
		$data['mode'] = "create";
		viewByTemplate('pro_tagihan_telephone/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Edit Tagihan Telephone";
		$data['data'] = $this->TagihanTelephone->GetSingleView(get('id_tagihan_tlp'));
		$data['id']= get('id_tagihan_tlp');

		$query1 = $this->db->query("SELECT id_tagihan_tlp FROM (SELECT * FROM pro_tagihan_telephone ORDER BY id_tagihan_tlp DESC) WHERE rownum <=1");
				 				if ($query1->row_array()) {
				 					$getquery1 = $query1->row_array();
				 					$rowcount1 = $getquery1['id_tagihan_tlp'];
				 					$autonum1 = $rowcount1 + 1;
				 				} else {
								$autonum1 = 1;
							}
		$data['id_tagihan_tlp_auto'] = $autonum1;

		$query3 = $this->db->query("SELECT id_tagihan_tlp_dtl FROM (SELECT * FROM pro_tagihan_telephone_dtl ORDER BY id_tagihan_tlp_dtl DESC) WHERE rownum <=1");
				 				if ($query3->row_array()) {
				 					$getquery3 = $query3->row_array();
				 					$rowcount3 = $getquery3['id_tagihan_tlp_dtl'];
				 					$autonum3 = $rowcount3 + 1;
				 				} else {
								$autonum3 = 1;
							}
		$data['id_tagihan_tlp_dtl_auto'] = $autonum3;

		$id_tagihan_tlp = get('id_tagihan_tlp');
		$query = $this->db->query("SELECT periode_pembayaran FROM pro_tagihan_telephone WHERE id_tagihan_tlp = '".$id_tagihan_tlp."'");
        if ($query->row_array()) {
        $getquery = $query->row_array();
        $key1 = $getquery['periode_pembayaran'];
        }
		$key = explode(" " , $key1);

		$data['bulan'] = $key[0];
		$data['tahun'] = $key[1];
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('pro_tagihan_telephone/form', $data, 'form');
	}	

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		//Validation		
		if (post("no_tagihan_tlp") == '0') {
			$error->Add("no_tagihan_tlp", "No Pembayaran", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_tagihan_telephone WHERE no_tagihan_tlp = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no_tagihan_tlp"))))."'")) > 0) {
			$error->Add("no_tagihan_tlp", "No Pembayaran", $tpl_duplicate);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	=  array(
					'no_tagihan_tlp' => $this->input->post('no_tagihan_tlp'),
					'id_tagihan_tlp' => $this->input->post('id_tagihan_tlp'),
					'nama_tenant' => $this->input->post('nama_tenant'),
					'atas_nama' => $this->input->post('atas_nama'),
					'alamat' => $this->input->post('alamat'),
					'periode_pembayaran' => $this->input->post('bulan')." ".$this->input->post('tahun'),
					'tanggal_bayar' => $this->input->post('tanggal_bayar')
					);

				$data['tanggal_bayar']	= ($data['tanggal_bayar'] == '' ? NULL : date_format3($data['tanggal_bayar'], 'YYYY-MM-DD'));

				if (post("mode") == "create")
				{
					$this->TagihanTelephone->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					$this->TagihanTelephone->Update($data, 
										array('id_tagihan_tlp'	=> $data['id_tagihan_tlp']));

					$ls_response = "Data Tersimpan [Edited]";
				}
			}
			// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];

				$query1 = $this->db->query("SELECT id_tagihan_tlp FROM (SELECT * FROM pro_tagihan_telephone ORDER BY id_tagihan_tlp DESC) WHERE rownum <=1");
				 				if ($query1->row_array()) {
				 					$getquery1 = $query1->row_array();
				 					$rowcount1 = $getquery1['id_tagihan_tlp'];
				 					$autonum1 = $rowcount1 + 1;
				 				} else {
								$autonum1 = 1;
							}
				$data['id_tagihan_tlp_auto'] = $autonum1;
				
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->db->delete('pro_tagihan_telephone_dtl', array('id_tagihan_tlp_dtl'=> $row['id_tagihan_tlp_dtl']));
				}
			
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$query = $this->db->query("SELECT id_tagihan_tlp_dtl FROM (SELECT * FROM pro_tagihan_telephone_dtl ORDER BY id_tagihan_tlp_dtl DESC) WHERE rownum <=1");
	 					if ($query->row_array()) {
	 						$getquery = $query->row_array();
	 						$rowcount = $getquery['id_tagihan_tlp_dtl'];
	 						$autonum = $rowcount + 1;
	 					} else {
					$autonum = 1;
				}

					$row = $dtl_sample2['inserted'][$i];
					$row_data['keterangan_tagihan'] = $row['keterangan_tagihan'];
					$row_data['biaya_tagihan'] = $row['biaya_tagihan'];
					$row_data['biaya_adm_bank'] = $row['biaya_adm_bank'];
					$row_data['id_tagihan_tlp_dtl'] = $autonum;
					
					if (post("mode") == "create"){
						$row_data['id_tagihan_tlp'] = $this->input->post('id_tagihan_tlp');
					}					
					else if (post("mode") == "edit"){
						$row_data['id_tagihan_tlp'] = $this->input->post('id_tagihan_tlp');
					}

					$this->db->insert('pro_tagihan_telephone_dtl',$row_data);
				}
					
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];		
					$row_data['keterangan_tagihan'] = $row['keterangan_tagihan'];
					$row_data['biaya_tagihan'] = $row['biaya_tagihan'];
					$row_data['biaya_adm_bank'] = $row['biaya_adm_bank'];							
					$this->db->update('pro_tagihan_telephone_dtl',$row_data, array('id_tagihan_tlp_dtl'=> $row['id_tagihan_tlp_dtl']));
				}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan			= "Data Terhapus";
		$id_tagihan_tlp 	= get('id_tagihan_tlp');
		
		if ($id_tagihan_tlp != "")
		{
			// Delete Single
			$arr_rule	= array('id_tagihan_tlp' => $id_tagihan_tlp);
			$ls_result = $this->TagihanTelephone->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_tagihan_tlp' => $row['id_tagihan_tlp']);
				$ls_result .= $this->TagihanTelephone->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}
}
?>